<?php
// 基本安全检查 - 仅允许本地访问
$remote_addr = $_SERVER['REMOTE_ADDR'] ?? '';
$allowed_ips = ['127.0.0.1', '::1', 'localhost'];

if (!in_array($remote_addr, $allowed_ips)) {
    header('HTTP/1.1 403 Forbidden');
    echo "Access denied. This page is only accessible from localhost.";
    exit;
}

// 显示基本的PHP配置信息
echo "<h1>PHP配置信息</h1>";
echo "<h2>基本信息</h2>";
echo "<ul>";
echo "<li>PHP版本: " . PHP_VERSION . "</li>";
echo "<li>服务器软件: " . ($_SERVER['SERVER_SOFTWARE'] ?? '未知') . "</li>";
echo "<li>操作系统: " . PHP_OS . "</li>";
echo "<li>当前时间: " . date('Y-m-d H:i:s') . "</li>";
echo "</ul>";

echo "<h2>重要配置</h2>";
echo "<ul>";
echo "<li>display_errors: " . ini_get('display_errors') . "</li>";
echo "<li>error_reporting: " . ini_get('error_reporting') . "</li>";
echo "<li>max_execution_time: " . ini_get('max_execution_time') . "</li>";
echo "<li>memory_limit: " . ini_get('memory_limit') . "</li>";
echo "<li>post_max_size: " . ini_get('post_max_size') . "</li>";
echo "<li>upload_max_filesize: " . ini_get('upload_max_filesize') . "</li>";
echo "<li>default_charset: " . ini_get('default_charset') . "</li>";
echo "</ul>";

echo "<h2>数据库扩展</h2>";
echo "<ul>";
echo "<li>PDO: " . (extension_loaded('pdo') ? '已启用' : '未启用') . "</li>";
echo "<li>PDO MySQL: " . (extension_loaded('pdo_mysql') ? '已启用' : '未启用') . "</li>";
echo "<li>MySQLi: " . (extension_loaded('mysqli') ? '已启用' : '未启用') . "</li>";
echo "</ul>";

echo "<h2>其他重要扩展</h2>";
echo "<ul>";
echo "<li>JSON: " . (extension_loaded('json') ? '已启用' : '未启用') . "</li>";
echo "<li>cURL: " . (extension_loaded('curl') ? '已启用' : '未启用') . "</li>";
echo "<li>GD: " . (extension_loaded('gd') ? '已启用' : '未启用') . "</li>";
echo "<li>OpenSSL: " . (extension_loaded('openssl') ? '已启用' : '未启用') . "</li>";
echo "</ul>";

echo "<h2>PHP错误日志</h2>";
$error_log = ini_get('error_log');
echo "<p>错误日志路径: " . ($error_log ? $error_log : '未设置或使用系统默认') . "</p>";

// 尝试连接数据库
echo "<h2>数据库连接测试</h2>";
try {
    require_once __DIR__ . '/config/database.php';
    echo "<p style='color: green;'>✓ 数据库连接成功</p>";
    
    // 检查表
    $tables = [];
    $stmt = $pdo->query("SHOW TABLES");
    while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
        $tables[] = $row[0];
    }
    
    echo "<p>数据库表 (" . count($tables) . "):</p>";
    echo "<ul>";
    foreach ($tables as $table) {
        echo "<li>" . htmlspecialchars($table) . "</li>";
    }
    echo "</ul>";
    
} catch (Exception $e) {
    echo "<p style='color: red;'>✗ 数据库连接失败: " . htmlspecialchars($e->getMessage()) . "</p>";
}

// 检查文件权限
echo "<h2>文件权限检查</h2>";
$directories = [
    '.' => '根目录',
    'config' => '配置目录',
    'ajax' => 'AJAX目录',
    'includes' => '包含文件目录',
    'static' => '静态文件目录'
];

echo "<table border='1' cellpadding='5'>";
echo "<tr><th>目录</th><th>描述</th><th>存在</th><th>可读</th><th>可写</th></tr>";

foreach ($directories as $dir => $description) {
    $exists = file_exists($dir);
    $readable = is_readable($dir);
    $writable = is_writable($dir);
    
    echo "<tr>";
    echo "<td>" . htmlspecialchars($dir) . "</td>";
    echo "<td>" . htmlspecialchars($description) . "</td>";
    echo "<td>" . ($exists ? '✓' : '✗') . "</td>";
    echo "<td>" . ($readable ? '✓' : '✗') . "</td>";
    echo "<td>" . ($writable ? '✓' : '✗') . "</td>";
    echo "</tr>";
}
echo "</table>";

// 检查重要文件
echo "<h2>重要文件检查</h2>";
$files = [
    'config/database.php' => '数据库配置',
    'includes/functions.php' => '核心函数库',
    'ajax/deposit.php' => '充值处理脚本',
    'deposit_payment.php' => '充值支付页面',
    'index.php' => '主页'
];

echo "<table border='1' cellpadding='5'>";
echo "<tr><th>文件</th><th>描述</th><th>存在</th><th>可读</th><th>大小</th><th>修改时间</th></tr>";

foreach ($files as $file => $description) {
    $exists = file_exists($file);
    $readable = is_readable($file);
    $size = $exists ? filesize($file) : 0;
    $modified = $exists ? date('Y-m-d H:i:s', filemtime($file)) : '-';
    
    echo "<tr>";
    echo "<td>" . htmlspecialchars($file) . "</td>";
    echo "<td>" . htmlspecialchars($description) . "</td>";
    echo "<td>" . ($exists ? '✓' : '✗') . "</td>";
    echo "<td>" . ($readable ? '✓' : '✗') . "</td>";
    echo "<td>" . number_format($size) . " 字节</td>";
    echo "<td>" . $modified . "</td>";
    echo "</tr>";
}
echo "</table>";

// 显示完整的phpinfo
echo "<h2>完整PHP信息</h2>";
echo "<p><a href='#' onclick='document.getElementById(\"phpinfo\").style.display=\"block\"; return false;'>点击显示完整PHP信息</a></p>";
echo "<div id='phpinfo' style='display:none;'>";
ob_start();
phpinfo();
$phpinfo = ob_get_clean();
// 移除头部和尾部HTML标签
$phpinfo = preg_replace('/<html.*?>|<\/html>|<head>.*?<\/head>|<body.*?>|<\/body>/s', '', $phpinfo);
echo $phpinfo;
echo "</div>";
?>
